package com.buka.service.impl;

import com.buka.http.AppInfo;
import com.buka.mapper.AdminMapper;
import com.buka.mapper.CinemaMapper;
import com.buka.mapper.MovieMapper;
import com.buka.mapper.UserMapper;
import com.buka.pojo.Admin;
import com.buka.pojo.Cinema;
import com.buka.pojo.Movie;
import com.buka.pojo.User;
import com.buka.service.AdminService;
import com.buka.utils.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author LiuHe
 * @version 1.0
 * @ClassName AdminServiceImpl
 * @date 2024/7/16 15:01
 */

/**
 * AdminServiceImpl 实现类
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private CinemaMapper cinemaMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 平台端登录
     */
    public ResponseJson login(String username, String password, HttpSession httpSession) {
        Admin admin = adminMapper.getAdminByUsername(username);
        if (admin != null) {
            if (admin.getAdminPassword().equals(password)) {
                httpSession.setAttribute(AppInfo.SESSION_ADMIN, admin);
                return ResponseJson.getOK();
            } else {
                return ResponseJson.getError("密码错误");
            }
        }
        return ResponseJson.getError("用户不存在");
    }

    /**
     * 添加电影
     */
    public ResponseJson addMovie(String movieName, int movieTime, MultipartFile file) throws IOException {
        String imgName = UUID.randomUUID().toString();
        file.transferTo(new File(AppInfo.MOVIE_IMG_PATH, imgName));
        Movie movie = new Movie(movieName, "http://localhost:8080/ssm/down/movie/img/" + imgName, movieTime);
        int i = movieMapper.add(movie);
        return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
    }

    /**
     * 查看电影列表
     */
    public ResponseJson getMovieList() {
        List<Movie> movieList = movieMapper.list();
        return ResponseJson.getOK(movieList);
    }

    /**
     * 添加影院
     */
    public ResponseJson addCinema(Cinema cinema) {
        int i = cinemaMapper.add(cinema);
        return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
    }

    /**
     * 影院列表
     */
    public ResponseJson getCinemaList() {
        List<Cinema> cinemaList = cinemaMapper.getCinemaList();
        return ResponseJson.getOK(cinemaList);
    }

    /**
     * 用户列表
     */
    public ResponseJson getUserList() {
        List<User> userList = userMapper.getUserList();
        return ResponseJson.getOK(userList);
    }

    /**
     * 封禁/解封 影院
     */
    public ResponseJson changeCinemaState(int cinemaId, int state) {
        if (state == 0 || state == 1) {
            int i = cinemaMapper.changeForStateById(cinemaId, state);
            return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
        }
        return ResponseJson.getError("影院状态异常");
    }

    /**
     * 封禁/解封 电影
     */
    public ResponseJson changeMovieState(int movieId, int state) {
        if (state == 0 || state == 1) {
            int i = movieMapper.changeForStateById(movieId, state);
            return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
        }
        return ResponseJson.getError("电影状态异常");
    }

    /**
     * 封禁/解封 用户
     */
    public ResponseJson changeUserState(int userId, int state) {
        if (state == 0 || state == 1) {
            int i = userMapper.changeForStateById(userId, state);
            return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
        }
        return ResponseJson.getError("电影状态异常");
    }
}
